<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="网站名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入网站名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="选择用户" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="选择用户"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="集群名称" prop="clusterId">
        <el-input
          v-model="queryParams.clusterId"
          placeholder="集群名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="选择分组" prop="groupId">-->
<!--        <el-input-->
<!--          v-model="queryParams.groupId"-->
<!--          placeholder="选择分组"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="绑定的域名" prop="domain">-->
<!--        <el-input-->
<!--          v-model="queryParams.domain"-->
<!--          placeholder="请输入绑定的域名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="绑定的端口，默认HTTP端口" prop="port">-->
<!--        <el-input-->
<!--          v-model="queryParams.port"-->
<!--          placeholder="请输入绑定的端口，默认HTTP端口"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="下行带宽，单位为Mbps" prop="downBandwidth">-->
<!--        <el-input-->
<!--          v-model="queryParams.downBandwidth"-->
<!--          placeholder="请输入下行带宽，单位为Mbps"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="当前QPS" prop="qps">-->
<!--        <el-input-->
<!--          v-model="queryParams.qps"-->
<!--          placeholder="请输入当前QPS"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="攻击流量QPS" prop="attackQps">-->
<!--        <el-input-->
<!--          v-model="queryParams.attackQps"-->
<!--          placeholder="请输入攻击流量QPS"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="源站地址，指向原始服务器的地址" prop="sourceAddress">-->
<!--        <el-input-->
<!--          v-model="queryParams.sourceAddress"-->
<!--          placeholder="请输入源站地址，指向原始服务器的地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="HTTPS证书ID，外键，关联 `certificates` 表" prop="httpsCertId">-->
<!--        <el-input-->
<!--          v-model="queryParams.httpsCertId"-->
<!--          placeholder="请输入HTTPS证书ID，外键，关联 `certificates` 表"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="网站创建者" prop="createdBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.createdBy"-->
<!--          placeholder="请输入网站创建者"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="网站创建时间" prop="createdTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.createdTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择网站创建时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="网站更新时间" prop="updatedTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.updatedTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择网站更新时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['website:server:add']"
        >创建网站</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['website:server:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['website:server:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['website:server:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="网站名称" align="center" prop="name" />
      <el-table-column label="所属用户" align="center" prop="userId" />
      <el-table-column label="部署集群" align="center">
        <template slot-scope="scope">
          {{ findClusterName(scope.row.clusterId) }}
        </template>
      </el-table-column>
      <el-table-column label="域名" align="center" prop="domain" />
      <el-table-column label="端口" align="center" prop="port" />
      <el-table-column label="下行带宽" align="center" prop="downBandwidth" />
      <el-table-column label="QPS" align="center" prop="qps" />
      <el-table-column label="攻击QPS" align="center" prop="attackQps" />
      <el-table-column label="网站状态" align="center" prop="status" />
<!--      <el-table-column label="网站创建时间" align="center" prop="createdTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="网站更新时间" align="center" prop="updatedTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['website:server:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['website:server:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网站信息，用于记录网站的基本信息，包括域名、端口、流量统计、证书等对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="网站名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入网站名称" />
        </el-form-item>
        <el-form-item label="所属用户ID，外键，关联 `edge_users` 表" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入所属用户ID，外键，关联 `edge_users` 表" />
        </el-form-item>
        <el-form-item label="部署集群ID，外键，关联 `edge_clusters` 表" prop="clusterId">
          <el-input v-model="form.clusterId" placeholder="请输入部署集群ID，外键，关联 `edge_clusters` 表" />
        </el-form-item>
        <el-form-item label="网站分组ID，外键，关联`edge_website_groups`" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入网站分组ID，外键，关联`edge_website_groups`" />
        </el-form-item>
        <el-form-item label="绑定的域名" prop="domain">
          <el-input v-model="form.domain" placeholder="请输入绑定的域名" />
        </el-form-item>
        <el-form-item label="绑定的端口，默认HTTP端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入绑定的端口，默认HTTP端口" />
        </el-form-item>
        <el-form-item label="下行带宽，单位为Mbps" prop="downBandwidth">
          <el-input v-model="form.downBandwidth" placeholder="请输入下行带宽，单位为Mbps" />
        </el-form-item>
        <el-form-item label="当前QPS" prop="qps">
          <el-input v-model="form.qps" placeholder="请输入当前QPS" />
        </el-form-item>
        <el-form-item label="攻击流量QPS" prop="attackQps">
          <el-input v-model="form.attackQps" placeholder="请输入攻击流量QPS" />
        </el-form-item>
        <el-form-item label="源站地址，指向原始服务器的地址" prop="sourceAddress">
          <el-input v-model="form.sourceAddress" placeholder="请输入源站地址，指向原始服务器的地址" />
        </el-form-item>
        <el-form-item label="HTTPS证书ID，外键，关联 `certificates` 表" prop="httpsCertId">
          <el-input v-model="form.httpsCertId" placeholder="请输入HTTPS证书ID，外键，关联 `certificates` 表" />
        </el-form-item>
        <el-form-item label="网站创建者" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入网站创建者" />
        </el-form-item>
        <el-form-item label="网站创建时间" prop="createdTime">
          <el-date-picker clearable
                          v-model="form.createdTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择网站创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="网站更新时间" prop="updatedTime">
          <el-date-picker clearable
                          v-model="form.updatedTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择网站更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServer, getServer, delServer, addServer, updateServer } from "@/api/website/server";
import {listClusters} from "@/api/nodes/clusters";

export default {
  name: "Server",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网站信息，用于记录网站的基本信息，包括域名、端口、流量统计、证书等表格数据
      serverList: [],
      // 集群信息
      clusterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        userId: null,
        clusterId: null,
        groupId: null,
        domain: null,
        port: null,
        downBandwidth: null,
        qps: null,
        attackQps: null,
        status: null,
        sourceAddress: null,
        httpsCertId: null,
        createdBy: null,
        createdTime: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "网站名称不能为空", trigger: "blur" }
        ],
        domain: [
          { required: true, message: "绑定的域名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    clusterNames() {
      return this.clusterList.reduce((acc, cluster) => {
        acc[cluster.id] = cluster.name;
        return acc;
      }, {});
    }
  },
  created() {
    this.getList();
    this.getClusterList();
  },
  methods: {
    /** 查询网站信息，用于记录网站的基本信息，包括域名、端口、流量统计、证书等列表 */
    getList() {
      this.loading = true;
      listServer(this.queryParams).then(response => {
        this.serverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     *  查询集群名称
     */
    getClusterList() {
      listClusters().then(response => {
        this.clusterList = response.data;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    findClusterName(clusterId) {
      return this.clusterNames[clusterId] || '未知集群';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        userId: null,
        clusterId: null,
        groupId: null,
        domain: null,
        port: null,
        downBandwidth: null,
        qps: null,
        attackQps: null,
        status: null,
        sourceAddress: null,
        httpsCertId: null,
        createdBy: null,
        createdTime: null,
        updateBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 跳转至/nodes/clusters/create
      this.$router.push({ name: 'CreateWebsite' }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getServer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网站信息，用于记录网站的基本信息，包括域名、端口、流量统计、证书等";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateServer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addServer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除网站信息，用于记录网站的基本信息，包括域名、端口、流量统计、证书等编号为"' + ids + '"的数据项？').then(function() {
        return delServer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('website/server/export', {
        ...this.queryParams
      }, `server_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
